package com.share.PACManager.task;

import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;


import org.json.JSONException;
import org.json.JSONObject;

import com.share.PACManager.model.CompanyModel;
import com.share.PACManager.model.TerminalModel;
import com.share.PACManager.net.HTTPResponse;
import com.share.PACManager.net.HttpUtility;
import com.share.PACManager.utils.Cryptography;
import com.share.PACManager.utils.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class AddCompanyTask extends Thread{

	private AddCompanyListener mListener;
	private Context mContext;	
	private TerminalModel mTerminal;
	private CompanyModel mCompany;
	private String mMessage;
	
	public interface AddCompanyListener {
		public TerminalModel requestTerminal();
		public CompanyModel requestCompany();
		public void onCheckResult(boolean result, CompanyModel company, String message);
	}

	public AddCompanyTask(Context context) {
		mContext = context;
	}
	
	public void start(AddCompanyListener listener) {
		mListener = listener;
		if(mListener != null)
		{
			mTerminal = mListener.requestTerminal();
			mCompany = mListener.requestCompany();
			start();
		}
	}
	
	private Handler handler = new Handler(){		
		@Override
		public void handleMessage(Message msg)
		{
			if(mListener != null)
			{
				if(msg.what != 0)
					mListener.onCheckResult(false, mCompany,mMessage);
				else
				{
					HTTPResponse resp = (HTTPResponse) msg.obj;
					if(!parse(resp.content))
						mListener.onCheckResult(false, mCompany,mMessage);
					else
					{
						mListener.onCheckResult(true, mCompany,mMessage);
					}
				}
			}				
		}
	};
	
    public boolean parse(String json)
    {
		try {
			JSONObject jsonObject = new JSONObject(json);
			mMessage = jsonObject.getString("Message");
			if(!jsonObject.getBoolean("Success"))
				return false;
			
			String companyid = Utils.getJsonString(jsonObject, "State");
			if(Utils.isEmptyString(companyid))
				return false;			
			mCompany.CompanyId = companyid;
			
			return true;
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }
    
	@Override
	public void run() {	

		try {
			String terminalId = mTerminal.TerminalId;
			String imei = mTerminal.TerminalCode;
			String password = mTerminal.TerminalPassword;
			String ticket = Cryptography.AES_Encode(imei, terminalId, password);
			
		
			String info = "";
			if(!Utils.isEmptyString(mCompany.CompanyId))
				info = "CompanyId=" + mCompany.CompanyId + "&" ;
			
			info +=	"CompanyType=" + Utils.URLEncode(mCompany.CompanyType) + "&" +
				"CompanyName=" + Utils.URLEncode(mCompany.CompanyName) + "&" +
				"CompanyAddress=" + Utils.URLEncode(mCompany.CompanyAddress) + "&" +
				"CompanyArea=" + Utils.URLEncode(mCompany.CompanyArea) + "&" +
				"CompanyLegalPerson=" + Utils.URLEncode(mCompany.CompanyLegalPerson) + "&" +
				"CompanySecurityPerson=" + Utils.URLEncode(mCompany.CompanySecurityPerson) + "&" +
				"CompanyPhoneNumber=" + Utils.URLEncode(mCompany.CompanyPhoneNumber) + "&" +
				"CompanyEastPoint=" + Utils.URLEncode(mCompany.CompanyEastPoint) + "&" +
				"CompanyNorthPoint=" + Utils.URLEncode(mCompany.CompanyNorthPoint) + "&" +
				"CompanyPoliceId=" + Utils.URLEncode(mCompany.CompanyPoliceId) + "&" +
				"CompanyTerminalId=" + Utils.URLEncode(mCompany.CompanyTerminalId) + "&" +
				"Comment=" + Utils.URLEncode(mCompany.CompanyComment);
			
			if(!Utils.isEmptyString(mCompany.CompanyOveralImage))
				info += "&CompanyOveralImage=" +  Utils.URLEncodeImage(mCompany.CompanyOveralImage);
			
			info = Cryptography.AES_Encode(info, terminalId, password);

			Map<String, String> params = new TreeMap<String, String>();
			params.put("Id", terminalId);
			params.put("TerminalId", terminalId);
			params.put("DataType", "Data");			
			params.put("Password", password);
			params.put("TerminalCode", imei);
			params.put("Version", "1.0");
			params.put("Ticket", ticket);
			params.put("Objective", "AddCompany");
			
			String entity = "Information=" + URLEncoder.encode(info);			
			handler.obtainMessage(0, HttpUtility.httpPost(params, entity)).sendToTarget();

		} catch (Exception e) {
			handler.obtainMessage(1).sendToTarget();
		}
		
		super.run();
	}	
	
	
}
